#!/usr/bin/env python
# encoding: utf-8
'''
@author: aliax
@license: (C) Copyright 2018-2020.
@contact: 1048327635@qq.com
@file: check_proxy.py
@time: 2020/7/8 0008 19:54
@desc:
'''
import time,random
import Config.config as con
import Config.db as db
import requests
import threading

class Check_proxy(object):

    def __init__(self):
        pass

    def check(self,hash):
        """
        验证代理是否可以用
        一次检测：从all中获取ip端口进行检测，成功放入useful，失败删除
        二次检测：从useful中获取，成功放入useful,失败删除
        :return:
        """
        while(1):
            #time.sleep(random.randint(5,20)) #随机等待5-20秒，等待抓取线程完成抓取
            time.sleep(1)
            #随机取5个ip进行验证
            try:
                all = db.keys(hash)
                choice = []
                choice.clear()
                #从redis all中随机抽取ip,放入choice列表中
                for i in range(1, 6):
                    p = random.choice(all)
                    choice.append(str(p.decode('utf-8')))
                #print(choice)
            except:
                con.log.error('Redis 代理获取失败，hash值：{}]'.format('all'))
                time.sleep(1)
            #从列表中遍历ip进行验证
            try:
                for i in choice:
                    thread_name = threading.currentThread().name
                    proxies = {
                        "http": "http://%(proxy)s/" % {'proxy': i},
                        "https": "http://%(proxy)s/" % {'proxy': i}
                    }
                    try:
                        req = requests.get(url=con.website.get('百度'), headers=con.headers, proxies=proxies,timeout=30)
                        if req.status_code == 200:
                            db.set('useful', i, i)
                            con.log.info('{}] - {} 代理 {} 检测成功'.format(thread_name,hash,i))
                    except:
                        con.log.error('{}] - {} 代理 {} 检测失败'.format(thread_name,hash,i))
                        db.delete(hash,i)
            except UnboundLocalError as e:
                con.log.error(e)

